package org.raymond.iworks.study.mq.rabbitmq.classic.work;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.MessageProperties;
import lombok.extern.slf4j.Slf4j;
import org.raymond.iworks.study.mq.rabbitmq.classic.hello.Send;

/**
 * @author raymond
 * @version V1.0
 * @Description: TODO
 */
@Slf4j
public class NewTask {
    public final static String QUEUE_NAME = "hello";
    public static void main(String[] args) throws Exception {
        String message = "1..............";
        // message = "interrupted..."; // 没有影响
        send(message);
    }

    public static void send(String message) throws Exception {
        try(Connection conn = Send.connection();
            Channel channel = conn.createChannel()){

            boolean durable = true; // 队列持久化,需要同时应用到生产者和消费者
            channel.queueDeclare(QUEUE_NAME, durable, false, false, null);
            // 将消息标记为持久消息,不太可靠:
            // 1,rabbitmq接收到消息一直到保存消息之间,存在一个很短的时间窗口可能会丢失消息
            // 2,不是每条消息都会通过fsync(2)同步到磁盘
            // 3,强持久需要用到publisher confirms
            channel.basicPublish("", QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
            log.info("[x] sent: {}", message);
        }
    }
}
